package com.nanbei.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanbei.commonUntil.R;
import com.nanbei.eduService.entity.EduCourse;
import com.nanbei.eduService.entity.vo.*;
import com.nanbei.eduService.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/eduService/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     *  添加课程基本信息
     * @param courseInfoVo
     * @return
     */
    @PostMapping("/addCourse")
    public R addCourse(@RequestBody CourseInfoVo courseInfoVo){
        //返回课程ID
        String id = eduCourseService.addCourse(courseInfoVo);
        return R.ok().data("courseID",id);
    }

    /**
     * 根据课程Id查询课程基本信息
     * @param courseId
     * @return
     */
    @GetMapping("findCourseById/{courseId}")
    public R findCourseById(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.findCourseById(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }


    @PostMapping("updateCourseById")
    public R updateCourseById(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseById(courseInfoVo);
        return R.ok();
    }


    /**
     * 根据ID查询发布课程的信息
     * @param courseId
     * @return
     */
    @GetMapping("selectCoursePublishVoById/{courseId}")
    public R selectCoursePublishVoById(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = eduCourseService.selectCoursePublishVoById(courseId);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }

    /**
     * 发布课程信息  // Normal代表课程以发布 ，Draft代表课程未发布
     * @param courseId
     * @return
     */
    @PostMapping("coursePublishCode/{courseId}")
    public R coursePublishCode(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal"); // Normal代表课程以发布 ，Draft代表课程未发布
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    /**
     * 条件查询带分页的方法
     *
     * @param current
     * @param limit
     * @param courseQueryVo  //查询条件
     * @return
     */
    @PostMapping("pageQueryCourse/{current}/{limit}")
    public R pageQueryCourse(@PathVariable long current,  //当前页码
                             @PathVariable long limit,   // 每页记录数
                             @RequestBody(required = false) CourseQueryVo courseQueryVo){

        Page<EduCourse> eduCoursePage = new Page<>(current,limit);

        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        String title = courseQueryVo.getTitle();
        String teacherId  = courseQueryVo.getTeacherId();
        String subjectParentId = courseQueryVo.getSubjectParentId();
        String subjectId = courseQueryVo.getSubjectId();

        if (!StringUtils.isEmpty(title)) {
            eduCourseQueryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId ) ) {
            eduCourseQueryWrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            eduCourseQueryWrapper.ge("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            eduCourseQueryWrapper.ge("subject_id", subjectId);
        }

        eduCourseService.page(eduCoursePage,eduCourseQueryWrapper);

        // 返回总记录数
        long total = eduCoursePage.getTotal();

        //数据List集合
        List<EduCourse> records = eduCoursePage.getRecords();

        return R.ok().data("total",total).data("records",records);
    }

    /**
     * 根据课程ID删除课程信息
     * @param courseId
     * @return
     */
    @DeleteMapping("/delCourseById/{courseId}")
    public R delCourseById(@PathVariable String courseId){
        eduCourseService.delCourseById(courseId);
        return R.ok();
    }

}

